package com.keyboardr.gamerater;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PlayerAdapter extends BaseAdapter {

	private final RaterActivity mOwner;
	private final LayoutInflater mInflater;

	public PlayerAdapter(RaterActivity owner) {
		super();
		mInflater = owner.getLayoutInflater();
		mOwner = owner;
	}

	@Override
	public int getCount() {
		return mOwner.getSortedList().size();
	}

	@Override
	public Object getItem(int position) {
		return mOwner.getSortedList().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout itemLayout;
		Player player = (Player) getItem(position);

		itemLayout = (RelativeLayout) mInflater.inflate(
				R.layout.player_list_item, parent, false);
		((TextView) itemLayout.findViewById(R.id.playerName)).setText(player
				.toString());
		((TextView) itemLayout.findViewById(R.id.playerRating)).setText(Integer
				.toString((int) (player.getRating() + 0.5)));

		return itemLayout;
	}

}
